#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;

int precompileFile(char* filename, OutputStream& output);
void outputLineInfo(OutputStream& output, char* filename, long line);


int main(int argc, char** argv)
{
  OutputStream output;
  for(int i=1;i<argc;++i){
    char* filename = argv[i];
    int result = precompileFile(filename,output);
    if(result != 0){
      cerr << "cannot not precompile file " << filename << endl;
      exit(-1);
    }
  }
  output.print();
  return 0;
}

int precompileFile(char* filename, OutputStream& output)
{
  long line = 1;
  outputLineInfo(output,filename,line);

  FILE* file = fopen(filename,"r");
  if(file == NULL){
    return -1;
  }
  Lexer lexer(file);

  Parser parser(lexer);

  parser.run();
  
  
  return 0;
}

void outputLineInfo(OutputStream& output, char* filename, long line)
{
  char buf[512];
  int len1 = strlen(filename);
  int len2 = sizeof(line);
  int len = len1 + len2 + 7;
  sprintf(buf,"# %ld \"%s\"\n",line,filename);
  output.append(buf,len);
}

